module.exports = (error, req, res, next) => {
  console.log("logger:" + error);

  let code = 500;
  let message = "服务器错误";
  let data = null;

  if (error.name === "SequelizeValidationError") {
    code = 400;
    message = "参数错误";
    data = error.errors.map((err) => err.message);
  }

  if (error.name === "BadRequestError") {
    code = 400;
    message = error.message === "Bad Request" ? "请求错误" : error.message;
  }

  if (error.name === "UnauthorizedError") {
    code = 401;
    message = error.message === "Unauthorized" ? "认证失败" : error.message;
  }

  if (error.name === "ConflictError") {
    code = 409;
    message = error.message === "Conflict" ? "资源冲突" : error.message;
  }

  if (error.name === "NotFoundError") {
    code = 404;
    message = error.message === "Not Found" ? "资源不存在" : error.message;
  }

  return res.status(code).json({
    status: false,
    code,
    message,
    data,
  });
};
